﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using SalePartial.Utility;
using System.Data;

namespace SalePartial.BLL
{
    public class ModuleItemBLL
    {
        public static bool AddItem1(string fi_no1, int fi_sort1, string fi_name1, int is_visible)
        {
            // 创建 SQL 修改数据的语句
            string SqlString = "insert into Func_Item1(fi_no1,fi_sort1,fi_name1,is_visible)  values (@fi_no1,@fi_sort1,@fi_name1,@is_visible)";
            try
            {
                using (SqlConnection Sql_Conn = new SqlConnection(GlobalSetting.ConnectionString))
                {

                    using (SqlCommand Sql_Command = new SqlCommand())
                    {

                        Sql_Conn.Open();


                        Sql_Command.Connection = Sql_Conn;
                        Sql_Command.CommandText = SqlString;

                        // 截取字符串到数据库所规范的大小 sfc.Left(string mdata, int leng)
                        Sql_Command.Parameters.AddWithValue("@fi_no1", SqlDbType.NVarChar).Value = fi_no1;
                        Sql_Command.Parameters.AddWithValue("@fi_sort1", SqlDbType.Int).Value = fi_sort1;
                        Sql_Command.Parameters.AddWithValue("@fi_name1", SqlDbType.NVarChar).Value = fi_name1;
                        Sql_Command.Parameters.AddWithValue("@is_visible", SqlDbType.Int).Value = is_visible;

                        Sql_Command.ExecuteNonQuery();


                    }
                }
            }
            catch (Exception e)
            {
                return false;
            }

            return true;
        }
 
        public static bool UpdateItem1(string fi_no1, int fi_sort1, string fi_name1, int is_visible)
        {
            // 创建 SQL 修改数据的语句
            string SqlString = "Update Func_Item1 Set  fi_sort1 = @fi_sort1, fi_name1 = @fi_name1, is_visible = @is_visible";
            SqlString = SqlString + " Where fi_no1 = @fi_no1";
            try
            {
                using (SqlConnection Sql_Conn = new SqlConnection(GlobalSetting.ConnectionString))
                {

                    using (SqlCommand Sql_Command = new SqlCommand())
                    {

                        Sql_Conn.Open();


                        Sql_Command.Connection = Sql_Conn;
                        Sql_Command.CommandText = SqlString;

                        // 截取字符串到数据库所规范的大小 sfc.Left(string mdata, int leng)
                        Sql_Command.Parameters.AddWithValue("@fi_no1", fi_no1);
                        Sql_Command.Parameters.AddWithValue("@fi_sort1", SqlDbType.Int).Value = fi_sort1;
                        Sql_Command.Parameters.AddWithValue("@fi_name1", SqlDbType.NVarChar).Value = fi_name1;
                        Sql_Command.Parameters.AddWithValue("@is_visible", SqlDbType.Int).Value = is_visible;

                        Sql_Command.ExecuteNonQuery();


                    }
                }
            }
            catch (Exception e)
            {
                return false;
            }

            return true;
        }

        public static bool DeleteItem1(string fi_no1)
        {
            DataTable dt = new DataTable();

            StringBuilder sb = new StringBuilder();

            try
            {
                using (SqlConnection Sql_Conn = new SqlConnection(GlobalSetting.ConnectionString))
                {

                    using (SqlCommand Sql_Command = new SqlCommand())
                    {
                        Sql_Conn.Open();


                        Sql_Command.Connection = Sql_Conn;

                        sb.Append("Delete From Func_Item1 Where fi_no1 = @fi_no1;");
                        sb.Append("Delete From Func_Item2 Where fi_no1=@fi_no1;");

                        Sql_Command.CommandText = sb.ToString();

                        Sql_Command.Parameters.AddWithValue("@fi_no1", fi_no1);
                        Sql_Command.ExecuteNonQuery();

                    }
                    Sql_Conn.Dispose();
                    Sql_Conn.Close();
                    Sql_Conn.Dispose();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static bool AddItem2(string fi_no2, string fi_no1, int fi_sort2, string fi_name2, string fi_url2, int is_visible)
        {
            // 创建 SQL 修改数据的语句
            string SqlString = "insert into Func_Item2(fi_no2,fi_no1,fi_sort2,fi_name2,fi_url2,is_visible) "+
                    "values (@fi_no2,@fi_no1,@fi_sort2,@fi_name2,@fi_url2,@is_visible)";
            try
            {
                using (SqlConnection Sql_Conn = new SqlConnection(GlobalSetting.ConnectionString))
                {

                    using (SqlCommand Sql_Command = new SqlCommand())
                    {

                        Sql_Conn.Open();


                        Sql_Command.Connection = Sql_Conn;
                        Sql_Command.CommandText = SqlString;

                        // 截取字符串到数据库所规范的大小 sfc.Left(string mdata, int leng)
                        Sql_Command.Parameters.AddWithValue("@fi_no2", SqlDbType.NVarChar).Value = fi_no2;
                        Sql_Command.Parameters.AddWithValue("@fi_no1", SqlDbType.NVarChar).Value = fi_no1;
                        Sql_Command.Parameters.AddWithValue("@fi_sort2", SqlDbType.Int).Value = fi_sort2;
                        Sql_Command.Parameters.AddWithValue("@fi_name2", SqlDbType.NVarChar).Value = fi_name2;
                        Sql_Command.Parameters.AddWithValue("@fi_url2", SqlDbType.NVarChar).Value = fi_url2;
                        Sql_Command.Parameters.AddWithValue("@is_visible", SqlDbType.Int).Value = is_visible;

                        Sql_Command.ExecuteNonQuery();


                    }
                }
            }
            catch (Exception e)
            {
                return false;
            }

            return true;
        }

        public static bool UpdateItem2(string fi_no2, int fi_sort2, string fi_name2, string fi_url2, int is_visible)
        {
            // 创建 SQL 修改数据的语句
            string SqlString = "Update Func_Item2 Set  fi_sort2 = @fi_sort2, fi_name2 = @fi_name2, fi_url2 = @fi_url2,is_visible = @is_visible";
            SqlString = SqlString + " Where fi_no2 = @fi_no2";
            try
            {
                using (SqlConnection Sql_Conn = new SqlConnection(GlobalSetting.ConnectionString))
                {

                    using (SqlCommand Sql_Command = new SqlCommand())
                    {

                        Sql_Conn.Open();


                        Sql_Command.Connection = Sql_Conn;
                        Sql_Command.CommandText = SqlString;

                        // 截取字符串到数据库所规范的大小 sfc.Left(string mdata, int leng)
                        Sql_Command.Parameters.AddWithValue("@fi_no2", fi_no2);
                        Sql_Command.Parameters.AddWithValue("@fi_sort2", SqlDbType.Int).Value = fi_sort2;
                        Sql_Command.Parameters.AddWithValue("@fi_name2", SqlDbType.NVarChar).Value = fi_name2;
                        Sql_Command.Parameters.AddWithValue("@fi_url2", SqlDbType.NVarChar).Value = fi_url2;
                        Sql_Command.Parameters.AddWithValue("@is_visible", SqlDbType.Int).Value = is_visible;

                        Sql_Command.ExecuteNonQuery();


                    }
                }
            }
            catch (Exception e)
            {
                return false;
            }

            return true;
        }

        public static bool DeleteItem2(string fi_no2)
        {
            DataTable dt = new DataTable();

            StringBuilder sb = new StringBuilder();

            try
            {
                using (SqlConnection Sql_Conn = new SqlConnection(GlobalSetting.ConnectionString))
                {

                    using (SqlCommand Sql_Command = new SqlCommand())
                    {
                        Sql_Conn.Open();


                        Sql_Command.Connection = Sql_Conn;

                        //sb.Append("Delete From Func_Item1 Where fi_no1 = @fi_no1;");
                        sb.Append("Delete From Func_Item2 Where fi_no2=@fi_no2;");

                        Sql_Command.CommandText = sb.ToString();

                        Sql_Command.Parameters.AddWithValue("@fi_no2", fi_no2);
                        Sql_Command.ExecuteNonQuery();

                    }
                    Sql_Conn.Dispose();
                    Sql_Conn.Close();
                    Sql_Conn.Dispose();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

    }
}
